package com.robusta.app.model.spreadsheet.loader;

import com.robusta.spreadsheet.describe.*;

import java.util.List;

import static com.google.common.collect.Iterables.getFirst;
import static com.google.common.collect.Lists.newArrayList;

public class WorkbookInfoFixture implements WorkbookInfo {

    private final WorksheetInfo worksheetInfo;

    public WorkbookInfoFixture() {
        worksheetInfo = new WorksheetInfoFixture();
    }

    @Override
    public List<TemplateInfo> templatesInfo() {
        return newArrayList(worksheetInfo.templateInfo());
    }

    @Override
    public List<? extends WorksheetInfo> worksheetsInfo() {
        return newArrayList(worksheetInfo);
    }

    public static class WorksheetInfoFixture implements WorksheetInfo {

        private final List<ColumnInfo> columnsInfoList;

        public WorksheetInfoFixture() {
            columnsInfoList = newArrayList();
            for(int i = 0; i < 30; i++) {
                columnsInfoList.add(new ColumnInfoFixture(i));
            }
        }

        @Override
        public TemplateInfo templateInfo() {
            return new TemplateInfo() {
                @Override
                public int getHeaderRowIndex() {
                    return 0;
                }

                @Override
                public int getSheetIndex() {
                    return 0;
                }

                @Override
                public int getStartColumnIndex() {
                    return 0;
                }
            };
        }

        @Override
        public List<? extends ColumnInfo> columnsInfo() {
            return columnsInfoList;
        }

        @Override
        public ColumnInfo keyColumn() {
            return getFirst(columnsInfoList, null);
        }
    }

    public static class ColumnInfoFixture implements ColumnInfo {
        private int index;

        public ColumnInfoFixture(int i) {
            this.index = i;
        }

        @Override
        public String modelFieldName() {
            return "field" + index;
        }

        @Override
        public String spreadsheetHeaderText() {
            return "header" + index;
        }

        @Override
        public Validator validator() {
            return new ValidatorFixture();
        }

        @Override
        public boolean isKey() {
            return index == 1;
        }
    }

    public static class ValidatorFixture implements Validator {
        @Override
        public ValidationResult runValidations(Validatable validatable) {
            return new ValidationResultFixture();
        }
    }

    public static class ValidationResultFixture implements ValidationResult {
        @Override
        public boolean isValid() {
            return true;
        }

        @Override
        public List<String> messages() {
            return newArrayList();
        }
    }
}
